/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import com.jeesite.modules.st.config.Constant;
import com.jeesite.modules.st.entity.StCustomRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StProductRecord;
import com.jeesite.modules.st.dao.StProductRecordDao;
import com.jeesite.modules.st.entity.StFormula;
import com.jeesite.modules.st.dao.StFormulaDao;
import com.jeesite.modules.st.entity.StFormulaName;
import com.jeesite.modules.st.dao.StFormulaNameDao;

/**
 * 产品档案Service
 *
 * @author HOHD
 * @version 2020-12-23
 */
@Service
@Transactional(readOnly = true)
public class StProductRecordService extends CrudService<StProductRecordDao, StProductRecord> {

    @Autowired
    private StFormulaDao stFormulaDao;

    @Autowired
    private StFormulaNameDao stFormulaNameDao;

    /**
     * 获取单条数据
     *
     * @param stProductRecord
     * @return
     */
    @Override
    public StProductRecord get(StProductRecord stProductRecord) {
        StProductRecord entity = super.get(stProductRecord);
        if (entity != null) {
            StFormula stFormula = new StFormula(entity);
            stFormula.setStatus(StFormula.STATUS_NORMAL);
            entity.setStFormulaList(stFormulaDao.findList(stFormula));
            StFormulaName stFormulaName = new StFormulaName(entity);
            stFormulaName.setStatus(StFormulaName.STATUS_NORMAL);
            entity.setStFormulaNameList(stFormulaNameDao.findList(stFormulaName));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param stProductRecord 查询条件
     * @return
     */
    @Override
    public Page<StProductRecord> findPage(StProductRecord stProductRecord) {
        return super.findPage(stProductRecord);
    }

    @Autowired
    StCustomRecordService stCustomRecordService;

    /**
     * 保存数据（插入或更新）
     *
     * @param stProductRecord
     */
    @Override
    @Transactional(readOnly = false)
    public void save(StProductRecord stProductRecord) {
        if (stProductRecord.getIsNewRecord()) {
            //插入产品编号
            String code;
            String productType = stProductRecord.getProductType();
            if (Constant.st_product_quality.custom_make != stProductRecord.getProductQuality()) {
                //非定制化就相加
                code = productType + stProductRecord.getAnimalCategory() + stProductRecord.getPercentIn()
                        + stProductRecord.getModel();
            } else {
                String customName = stProductRecord.getCustomName();
                StCustomRecord stCustomRecord = stCustomRecordService.get(customName);
                String customCode = stCustomRecord.getCustomCode();
                Integer number = stProductRecord.getNumber();
                code = productType + customCode + number;
            }
            stProductRecord.setProductCode(code);
        }
        super.save(stProductRecord);
        // 保存 StProductRecord子表
        for (StFormula stFormula : stProductRecord.getStFormulaList()) {
            if (!StFormula.STATUS_DELETE.equals(stFormula.getStatus())) {
                stFormula.setProductId(stProductRecord);
                if (stFormula.getIsNewRecord()) {
                    stFormulaDao.insert(stFormula);
                } else {
                    stFormulaDao.update(stFormula);
                }
            } else {
                stFormulaDao.delete(stFormula);
            }
        }
        // 保存 StProductRecord子表
        for (StFormulaName stFormulaName : stProductRecord.getStFormulaNameList()) {
            if (!StFormulaName.STATUS_DELETE.equals(stFormulaName.getStatus())) {
                stFormulaName.setProductId(stProductRecord);
                if (stFormulaName.getIsNewRecord()) {
                    stFormulaNameDao.insert(stFormulaName);
                } else {
                    stFormulaNameDao.update(stFormulaName);
                }
            } else {
                stFormulaNameDao.delete(stFormulaName);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param stProductRecord
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(StProductRecord stProductRecord) {
        super.updateStatus(stProductRecord);
    }

    /**
     * 删除数据
     *
     * @param stProductRecord
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(StProductRecord stProductRecord) {
        super.delete(stProductRecord);
        StFormula stFormula = new StFormula();
        stFormula.setProductId(stProductRecord);
        stFormulaDao.deleteByEntity(stFormula);
        StFormulaName stFormulaName = new StFormulaName();
        stFormulaName.setProductId(stProductRecord);
        stFormulaNameDao.deleteByEntity(stFormulaName);
    }

}